package com.zyx.xunyilu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zyx.xunyilu.config.jwt.JWTUtils;
import com.zyx.xunyilu.entity.Article;
import com.zyx.xunyilu.entity.CommentArticle;
import com.zyx.xunyilu.entity.User;
import com.zyx.xunyilu.service.CommentArticleService;
import com.zyx.xunyilu.service.UserService;
import com.zyx.xunyilu.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @ClassName: CommentArticleController
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2019/12/16 21:56
 */
@CrossOrigin
@RestController
@RequestMapping("/commentarticle")
public class CommentArticleController {

    @Autowired
    CommentArticleService commentArticleService;

    @Autowired
    JWTUtils jwtUtils;

    @Autowired
    UserService userService;

    // 用户接口开始

    /**
     * 对文章添加评论
     * 追评的评论id会被置为null
     * @param token 评论者的token
     * @param commentArticle    追加的评论
     * @return  返回评论内容
     */
    @PostMapping("/insert")
    public RestResponse insertComment(@RequestAttribute("token") String token,
                                      @Validated(CommentArticle.commentToArticle.class) CommentArticle commentArticle,
                                      BeanPropertyBindingResult e) throws Exception {
        if (e.hasErrors()) {
            throw new Exception(e.getAllErrors().get(0).getDefaultMessage());
        }
        commentArticle.setCId(null);
        commentArticle.setTime(new Date());
        CommentArticle result = commentArticleService.insert(commentArticle, token);
        return RestResponse.ok(result);
    }

    /**
     * 对评论的追评
     * @param token 发布者token
     * @param commentArticle    评论内容
     */
    @PostMapping("/comment")
    public RestResponse insertCommentToOne(@RequestAttribute("token") String token,
                                           @Validated(CommentArticle.commentToComment.class) CommentArticle commentArticle,
                                           BeanPropertyBindingResult e) throws Exception {
        if (e.hasErrors()) {
            throw new Exception(e.getAllErrors().get(0).getDefaultMessage());
        }
        commentArticle.setTime(new Date());
        CommentArticle insert = commentArticleService.insert(commentArticle, token);
        return RestResponse.ok(insert);
    }


    @GetMapping("/select/comment/{id}")
    public RestResponse selectById(@PathVariable(value = "id") Integer id) {
        CommentArticle commentArticle = commentArticleService.selectById(id);
        return RestResponse.ok(commentArticle);
    }

    /**
     * 获取评论的追评分页列表
     * @param page  页码
     * @param size  每页尺寸
     * @param cId   追评目标评论的id
     * @return  返回分页列表
     */
    @GetMapping("/select/comment/page")
    public RestResponse selectCommentByCommentId(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                                                 @Validated @NotNull(message = "好好传参数!") @RequestParam("cId") Integer cId) {
        IPage<CommentArticle> commentArticleIPage = commentArticleService.selectByCId(cId, page, size);
        return RestResponse.ok(commentArticleIPage);
    }

    /**
     * 获取文章的评论分页
     * @param page  页码
     * @param size  每页尺寸
     * @param pId   评论目标文章的id
     * @return  返回分页列表
     */
    @GetMapping("/select/article/page")
    public RestResponse selectArticlePage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                                          @Validated @NotNull(message = "好好传参数!") @RequestParam("pId") Integer pId) {
        IPage<CommentArticle> commentArticleIPage = commentArticleService.selectArticleCommentPage(pId, page, size);
        return RestResponse.ok(commentArticleIPage);
    }

    @PostMapping("/my")
    public RestResponse selectCommonArticle(@RequestAttribute("token") String token,
                                            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String username = jwtUtils.getUsername(token);
        User userByName = userService.findUserByName(username);
        IPage<Article> articleIPage = commentArticleService.selectMyCommonArticlePage(userByName.getId(), page, size);
        return RestResponse.ok(articleIPage);
    }

    // 管理员接口
}
